package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.pb.activity.HangHaiActivityProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.hanghai.HangHaiModule;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.CrossProtocol;

import java.util.List;

public class UnionHangHaiListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        boolean memberChange = syncType == eUnionSyncType.JoinUnion.getValue()
                || syncType == eUnionSyncType.BeKick.getValue()
                || syncType == eUnionSyncType.QuitUnion.getValue();
        if (!memberChange) {
            return;
        }

        int type = syncType == eUnionSyncType.JoinUnion.getValue() ? 1 : 2; // 1加入，2退出（被踢出）
        CrossUnionMember changeMember = memberList.get(0);

        if (changeMember.getServerId() != GameServer.getInstance().getServerId()) {
            return;
        }

        HangHaiConfig config = HangHaiMgr.getHangHaiConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            return;
        }

        int groupId = GameUnionActivityGroupMgr.getGroupId(config.getActivityInfo().getActivityId(), unionInfo.getUnionUid());

        HangHaiActivityProto.CrossHangHaiUnionActionReqMsg.Builder builder = HangHaiActivityProto.CrossHangHaiUnionActionReqMsg.newBuilder();
        builder.setActivityId(config.getActivityId());
        builder.setUnionUid(unionInfo.getUnionUid());
        builder.setUserId(changeMember.getUserId());
        builder.setType(type); // 1加入，2退出
        GamePlayerMgr.sendUnionActivityGroupPacket(CrossProtocol.C2_HANGHAI_ACTIVITY_UNION_ACTION, builder, 0, config.getActivityId(), groupId);

        // 在线加入，生成数据
        if (syncType == eUnionSyncType.JoinUnion.getValue()) {
            GamePlayer player = GamePlayerMgr.getOnlinePlayer(changeMember.getUserId());
            if (player != null) {
                HangHaiModule module = player.getModule(HangHaiModule.class);

                HangHaiActivityProto.CrossHangHaiLoginReqMsg.Builder loginBuilder = HangHaiActivityProto.CrossHangHaiLoginReqMsg.newBuilder();
                loginBuilder.setActivityId(config.getActivityInfo().getActivityId());
                loginBuilder.setUnionUid(player.getUserInfo().getUnionUid());
                loginBuilder.setPlayer(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo()));
                loginBuilder.setInitMoney(module.calInitMoney(config));
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_HANGHAI_ACTIVITY_LOGIN, loginBuilder, config.getActivityId(), groupId);
            }
        }

    }
}
